package com.netease.yunxin.app.im.main

import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import androidx.core.app.ServiceCompat
import com.lxj.xpopup.XPopup
import com.netease.yunxin.app.im.BaseApplication
import com.netease.yunxin.app.im.service.SqueezeService
import com.netease.yunxin.app.im.utils.FileUtil
import com.netease.yunxin.app.im.utils.NotificationUtil
import com.netease.yunxin.kit.common.ui.activities.BaseActivity
import com.netease.yunxin.kit.common.utils.FileUtils
import com.netease.yunxin.kit.common.utils.UriUtils
import kt.toast
import java.io.File


abstract class ShareDelegateActivity : BaseActivity() {
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        save(intent)
    }

    private fun save(intent: Intent?) {
        intent?.data?.let { uri ->
            val realPath = UriUtils.uri2FileRealPath(uri)
            val fileName = FileUtil.getFileName(realPath)
            val desPath =
                BaseApplication.sInstance.externalCacheDir.toString() + File.separator + "salton"
            FileUtils.copy(realPath, desPath + File.separator + fileName)
            toast("接收到文件:$fileName")
        }
    }

    // Our PlayerService
    private lateinit var notificationService: SqueezeService
    private var sBound = false
    private lateinit var mBindingIntent: Intent

    // Defines callbacks for service binding, passed to bindService()
    private val connection = object : ServiceConnection {
        override fun onServiceConnected(componentName: ComponentName, service: IBinder) {
            // get bound service and instantiate MediaPlayerHolder
            val binder = service as SqueezeService.LocalBinder
            notificationService = binder.getService()
            val intent = Intent(this@ShareDelegateActivity, MainActivity::class.java)
            val pendingIntent = PendingIntent.getActivity(this@ShareDelegateActivity, 11, intent,PendingIntent.FLAG_MUTABLE)
            notificationService.helper.pendingIntent = pendingIntent
            sBound = true
        }

        override fun onServiceDisconnected(componentName: ComponentName) {
            sBound = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!NotificationUtil.areNotificationsEnabled(this)) {
            XPopup.Builder(this).asConfirm("提示", "打开通知不错过好友消息") {
                NotificationUtil.toSetting(this)
            }.show()
        }
    }
    override fun onStart() {
        super.onStart()
        initNotification()
    }

    override fun onResume() {
        super.onResume()
        initNotification()
    }

    fun initNotification() {
        mBindingIntent = Intent(this, SqueezeService::class.java).also {
            bindService(it, connection, Context.BIND_AUTO_CREATE)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (::notificationService.isInitialized && notificationService.isRunning) {
            ServiceCompat.stopForeground(notificationService, ServiceCompat.STOP_FOREGROUND_REMOVE)
            stopService(mBindingIntent)
            if (sBound) unbindService(connection)
        }
    }

}